#Written by MMCOWBOY
import pygame, os
import game

class cursor:
      def __init__(self):
#            self.image = pygame.image.load("ttt.png")
#            self.imagerect = self.image.get_rect(0,0,1,1)
            self.imagerect = pygame.Rect(0,0,1,1)

      def update(self):
            self.imagerect.topleft = pygame.mouse.get_pos()

class menu_item:
      def __init__(self, text, pos, cursor, command = ""):
            self.text = text
            self.font = pygame.font.Font(pygame.font.get_default_font(), 25)
            self.txtimage = self.font.render(self.text, True, (255, 255, 255))
            self.txtrect = self.txtimage.get_rect()
            self.txtrect.topleft = pos
            self.command = command
            self.cursor = cursor
            self.heightlighted = False

      def update(self):
            if (self.txtrect.colliderect(self.cursor.imagerect)):
                  self.heightlighted = True
            else:
                  self.heightlighted = False

      def detctselect(self,event):
            if (event.type == pygame.MOUSEBUTTONDOWN):
                  if (event.button == 1):
                        if (self.cursor.imagerect.colliderect(self.txtrect)):
                              return 1            

      def show(self, screen):
            if (self.heightlighted):
                  ttxtimage = self.font.render(self.text, True, (255, 255, 128))
                  screen.blit(ttxtimage, self.txtrect)
            else:
                  screen.blit(self.txtimage, self.txtrect)

class menu:
      def __init__(self,screen):

            self.cursor = cursor()

            self.clock = pygame.time.Clock()

            xoffset = 165
            yoffset = 250

            self.options = []

            self.options.append(menu_item("1 Player Game", (xoffset,yoffset + (0 * 35)), self.cursor, "1P"))
            self.options.append(menu_item("2 Player Game", (xoffset,yoffset + (1 * 35)), self.cursor, "2P"))
            self.options.append(menu_item("Back to main menu", (xoffset,yoffset + (2 * 35)), self.cursor, "BACK"))
            
            
            while 1:
                  self.clock.tick(50)

                  events = pygame.event.get()
                  for e in events:
                        if e.type == pygame.QUIT:
                              pygame.quit()
                              return
                        if e.type == pygame.KEYDOWN:
                              if e.key == pygame.K_ESCAPE:
                                    pygame.quit()
                                    return

                        if (self.options[0].detctselect(e)):
                              print "1 Player Game"
                              _game = game.game_obj(screen, 1)
                              _game.run_game()
#                              newtest.rungame(screen,"1P")

                        if (self.options[1].detctselect(e)):
                              print "2 Player Game"
                              _game = game.game_obj(screen, 2)
                              _game.run_game()
#                              newtest.rungame(screen,"2P")

                        if (self.options[2].detctselect(e)):
                              print "Back to main menu"
#                              pygame.quit()
                              return

                  screen.fill((0, 0, 0))

                  for op in self.options:
                        op.update()
                        op.show(screen)
                  
                  self.cursor.update()

#                  screen.blit(self.cursor.image, self.cursor.imagerect)

                  pygame.display.flip()
